package com.zf.seata.seatastorage.controller;

import com.zf.common.core.result.RestResponse;
import com.zf.seata.seatastorage.entity.ProdStorage;
import com.zf.seata.seatastorage.service.ProdStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Zhou Feng
 */
@RestController
@RequestMapping(value = "/storage")
public class ProdStorageController {
    @Autowired
    private ProdStorageService storageService;


    @PostMapping("/changeStorage")
    public RestResponse<?> changeStorage(@RequestParam(name = "id") Long id, @RequestParam("num") Integer num){
        ProdStorage prodStorage = storageService.getById(id);
        if (prodStorage.getTotal() >= num){
            prodStorage.setTotal(prodStorage.getTotal()-num);
            storageService.updateById(prodStorage);
            return RestResponse.success();
        }else {
            throw new RuntimeException("库存不足");
        }
    }
}
